package com.zbcbms.controller;

import com.alibaba.druid.util.StringUtils;
import com.zbcbms.entity.dto.UserMap;
import com.zbcbms.entity.vo.UserVo;
import com.zbcbms.service.UserService;
import com.zbcbms.service.impl.UserServiceImpl;
import com.zbcbms.Main;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;


/**
 * @author fengq
 */
public class LoginController implements Initializable {


    @FXML
    private Button cancelBtn;

    @FXML
    private TextField accountTextField;

    @FXML
    private PasswordField passwordTextField;

    @FXML
    private Label tipMessageLabel;

    private UserService userService=new UserServiceImpl();


    /**
     * 登陆按钮监听事件的绑定
     * @param event
     */
    @FXML
    public void loginBtnOnAction(ActionEvent event){
        String account=accountTextField.getText();
        String password=passwordTextField.getText();
        if (StringUtils.isEmpty(account) ||StringUtils.isEmpty(password)){
            tipMessageLabel.setText("用户名或密码不能为空");
        }else if (password.length()<6){
            tipMessageLabel.setText("密码长度小于6位");
        } else {
            String queryPassword = userService.getPasswordByNumber(account);
            if (StringUtils.isEmpty(queryPassword)){
                tipMessageLabel.setText("用户不存在");
            }else if (queryPassword.equals(password)){
                this.cancelBtnOnAction();
                UserMap.setNumber(account);
                UserMap.setPassword(password);
                UserVo userVo = userService.getUserByNumber(account);
                UserMap.setName(userVo.getUserName());
                UserMap.setRole(userVo.getRole());
                UserMap.setJoinTime(userVo.getJoinDate());
                Stage h = new Main().homePageInit();
                tipMessageLabel.setText("登陆成功！！");
                h.showAndWait();
            }else {
                tipMessageLabel.setText("密码错误");
            }

        }

    }

    /**
     * 退出界面的监听
     */
    public void cancelBtnOnAction() {
        Stage stage = (Stage) cancelBtn.getScene().getWindow();
        stage.close();
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
